<?php
/**
* @package Pages-and-Items (com_pi_pages_and_items)
* @version 1.5.7
* @copyright Copyright (C) 2006-2010 Carsten Engel. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author http://www.pages-and-items.com
*/
	
//no direct access
if(!defined('_VALID_MOS') && !defined('_JEXEC')){
	die('Restricted access');
}

function do_install_update_script(){
	global $database;

	//get database
	if(defined('_JEXEC')){
		//joomla 1.5
		$database = JFactory::getDBO();
	}	
	
	//table for item index
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__pi_item_index (
  `id` int(11) NOT NULL auto_increment,
  `item_id` int(11) NOT NULL,
  `itemtype` tinytext NOT NULL,
  `show_title` tinyint(4) NOT NULL,
  PRIMARY KEY  (`id`)
) ");
$database->query();
	
	//table for downloads
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__pi_downloads (
  `id` int(11) NOT NULL auto_increment,
   `file_name` tinytext NOT NULL,
  `access` tinyint(4) NOT NULL default '0',
  `type` tinytext NOT NULL,
  `hits` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
)");
	$database->query();
	
	
	//table for custom itemtypes
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__pi_customitemtypes (
  `id` int(11) NOT NULL auto_increment,
  `name` tinytext NOT NULL,
  `read_more` varchar(1) NOT NULL,
  `template_intro` text NOT NULL,
  `template_full` text NOT NULL,
  `editor_id` INT NOT NULL,  
  `html_after` text NOT NULL,
  `html_before` text NOT NULL,
  PRIMARY KEY  (`id`)
)");
	$database->query();
	
	$database->setQuery("SHOW COLUMNS FROM #__pi_customitemtypes");
	$columns = $database->loadResultArray();	
	if(!in_array('editor_id', $columns)){
		$database->setQuery("ALTER TABLE #__pi_customitemtypes ADD `editor_id` INT NOT NULL AFTER `template_full`");
		$database->query();
	}
	if(!in_array('html_after', $columns)){
		$database->setQuery("ALTER TABLE #__pi_customitemtypes ADD `html_after` text NOT NULL AFTER `editor_id`");
		$database->query();
	}
	if(!in_array('html_before', $columns)){
		$database->setQuery("ALTER TABLE #__pi_customitemtypes ADD `html_before` text NOT NULL AFTER `html_after`");
		$database->query();
	}
	
	//table for custom itemtype fields
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__pi_custom_fields (
  `id` int(11) NOT NULL auto_increment,
  `name` tinytext NOT NULL,
  `type_id` int(11) NOT NULL,
  `plugin` tinytext NOT NULL,
  `ordering` int(11) NOT NULL,  
  `params` text NOT NULL,
  PRIMARY KEY  (`id`)
)");
	$database->query();
	
	
	//table for custom itemtype fields-values
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__pi_custom_fields_values (
  `id` int(11) NOT NULL auto_increment,
  `field_id` int(11) NOT NULL,
  `item_id` int(11) NOT NULL,
  `value` text NOT NULL,
  PRIMARY KEY  (`id`)
)");
	$database->query();
	
	
	//table for itemtype other_item
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__pi_item_other_index (
  `id` int(11) NOT NULL auto_increment,
  `item_id` int(11) NOT NULL,
  `other_item_id` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
)");
	$database->query();

	//table for configuration
	$database->setQuery("CREATE TABLE IF NOT EXISTS #__pi_config (
  `id` varchar(255) NOT NULL,
  `config` text NOT NULL,
  PRIMARY KEY  (`id`)  
)");
	$database->query();
	
	//check if config is empty, if so insert default config
	$database->setQuery("SELECT id FROM #__pi_config WHERE id='pi' ");
	$pi_config_id = $database->loadResult();
	if(!$pi_config_id){
		$configuration = 'language=english
use_pi_frontend_editting=true
menus=mainmenu;Main Menu
cit=1
use_user_access_component=false
item_props_publish=true
item_show_frontpage_option=true
item_menu=tabs
item_props_alias=true
plugin_syntax_cheatcheat=
allow_authors_to_publish=false
allow_editors_to_publish=false
item_save_redirect=item
make_article_alias_unique=true
date_format=%d-%m-%Y
create_sef_urls=
sef_url_cat=true
sef_url_id=true
sef_url_ext=
item_save_redirect_url=
item_props_title=true
item_props_access=true
item_props_author_alias=true
item_props_author=true
item_props_created_date=true
item_props_publish_up=true
item_props_publish_down=true
item_props_hits=true
item_props_version=true
item_props_created=true
item_props_modified=true
item_props_item_id=true
item_props_category_id=true
item_props_section_id=true
item_props_other_items=true
item_props_show_title=true
item_props_linked_titles=true
item_props_intro=true
item_props_section=true
item_props_section_link=true
item_props_category=true
item_props_category_link=true
item_props_rating=true
item_props_author_names=true
item_props_date=true
item_props_mod_date=true
item_props_pdf_link=true
item_props_print_link=true
item_props_mail_link=true
item_props_language=true
item_props_key=true
item_props_readmore=true
item_props_description=true
item_props_keywords=true
item_props_robots=true
item_props_metaauthor=true
item_props_hideforsuperadmin=true
item_new_frontpage=false
item_new_show_title=true
item_new_state=true
item_new_access=0
item_language=en-GB
item_readmore=
item_type_select_frontend=true
item_type_not_text_frontend=true
item_type_not_text_frontend_edit=true
new_item_section_category_select=pages
image_maxsize=8000000
image_filetypes=gif,jpg
image_dir=../images/stories/
image_max_width=400
image_max_height=500
download_allow=true
download_filetypes=jpg,gif,txt,pdf,doc,zip,rar,odt
download_maxsize=8000000
download_dir=../media/
download_hits=true
download_hitsreset=true
download_debugger=false
sections=
inherit_from_parent=true
sections_from_db=true
inherit_from_parent_move=true
child_inherit_from_parent_move=true
child_inherit_from_parent_change=true
make_page_alias_unique=
truncate_item_title=0
page_props_publish=true
page_props_section=true
page_props_alias=true
page_props_title=true
page_props_title_visible=true
page_props_show_in_menu=true
page_props_access=true
page_props_defaulthome=true
page_props_leading=true
page_props_nr_intro=true
page_props_columns=true
page_props_nr_links=true
page_props_cat_order=true
page_props_primaryorder=true
page_props_pagination=true
page_props_paginationresults=true
page_props_feedlink=true
page_props_shownoauth=true
page_props_categorydescription=true
page_props_categorydescriptionimage=true
page_props_categoryname=true
page_props_categorynameclickable=true
page_props_itemtitles=true
page_props_showintro=true
page_props_section_title=true
page_props_sectionlink=true
page_props_linkedtitles=true
page_props_readmore=true
page_props_itemrating=true
page_props_authornames=true
page_props_createdtimedate=true
page_props_modifiedtimedate=true
page_props_showitemnavigation=true
page_props_show_icons=true
page_props_pdf_link=true
page_props_print=true
page_props_mail_link=true
page_props_showhits=true
page_props_feedsummary=true
page_props_suffix=true
page_props_secure=true
page_props_sectionid=true
page_props_catid=true
page_props_menuid=true
page_props_hideforsuperadmin=true
page_new_publish_category=1
page_new_publish_menu=true
page_new_access_menu=0
page_new_access_category=0
itemtypes=html,other_item,text
';

		if(defined('_JEXEC')){
			//joomla 1.5
			$configuration .= 'START_PAGE_NEW_ATTRIBUTES=
show_page_title=0
num_leading_articles=0
num_intro_articles=999
num_columns=1
num_links=0
orderby_pri=
orderby_sec=order
show_pagination=2
show_pagination_results=0
show_feed_link=0
show_noauth=2
show_description=0
show_description_image=0
show_category=0
link_category=0
show_title=2
show_intro=
show_section=
link_section=
link_titles=
show_readmore=
show_vote=
show_author=
show_create_date=
show_modify_date=
show_item_navigation=
show_icons=
show_pdf_icon=
show_print_icon=
show_email_icon=
show_hits=
feed_summary=
pageclass_sfx=
secure=-1
menu_image=-1';
		}else{
			//joomla 1.0.x
			$configuration .= 'START_PAGE_NEW_ATTRIBUTES=
menu_image=-1
pageclass_sfx=
back_button=
header=
page_title=
leading=0
intro=999
columns=1
link=100
orderby_pri=order
orderby_sec=order
pagination=0
pagination_results=0
image=1
description=0
description_image=0
category=0
category_link=0
item_title=1
link_titles=
readmore=
rating=
author=
createdate=
modifydate=
pdf=
print=
email=
categoryid=
unpublished=';
		}
		
		//insert fresh config
		$database->setQuery( "INSERT INTO #__pi_config SET id='pi', config='$configuration'");
		$database->query();
	}
	
	/*
	Carsten 16-9-2009. sorry Micha. Taken out temporarily
	//ADD MS 13.09.2009
	//check table for template configuration template
	//TODO i have not check if this work correct in install PI only in config_custom_itemtype.php
	$class_name = 'check_config_template';
	require_once(dirname(__FILE__).'/class_check_config_template.php');
	$class_template = new $class_name($class_pi->db);
	//ADD MS END 13.09.2009	
	*/

	//icon stuff
	if( defined('_JEXEC') ){
		//joomla 1.5
		$icon_path = 'components';
	}else{
		//joomla 1.0.x
		$icon_path = '../administrator/components';
	}	
	
	//do icon
	$database->setQuery("UPDATE #__components SET admin_menu_img='$icon_path/com_pi_pages_and_items/images/icon.gif' WHERE link='option=com_pi_pages_and_items'");
	$database->query();
	
	/*
	//1.5.2
	//check if date format really is in config, else update
	$database->setQuery("SELECT config FROM #__pi_config WHERE id='pi' ");
	$pi_config_raw = $database -> loadResult();
	if(!strpos($pi_config_raw, 'date_format=')){
		$new_config = 'date_format=%d-%m-%Y
'.$pi_config_raw;
		//update config
		$database->setQuery( "UPDATE #__pi_config SET config='$new_config' WHERE id='pi' "	);
		$database->query();
	}
	*/
	
	//get config		
	$database->setQuery("SELECT config "
	."FROM #__pi_config "
	."WHERE id='pi' "
	."LIMIT 1"
	);		
	$raw = $database->loadResult();		
	
	$updated_config = $raw;	
			
	//check for missing config vars
	$config_needs_updating = 0;	
	
	//added in version 1.5.2
	//check if date format really is in config, else update
	if(!strpos($raw, 'date_format=')){
		$updated_config = $updated_config.'
date_format=%d-%m-%Y
make_article_alias_unique=
';
		$config_needs_updating = 1;
	}
	
	//added in version 1.5.4
	//get page attributes		
	$pos_start_page_attribs = strpos($raw, 'START_PAGE_NEW_ATTRIBUTES');
	$start_of_vars = $pos_start_page_attribs+26;
	$page_new_attribs = substr($raw, $start_of_vars, 99999);		
	$pi_config['page_new_attribs'] = $page_new_attribs;		
	
	//get just the config vars
	$rest_of_config = substr($raw, 0, $pos_start_page_attribs);
	
	$params = explode( "\n", $rest_of_config);
	
	for($n = 0; $n < count($params); $n++){		
		$temp = explode('=',$params[$n]);
		$var = $temp[0];
		$value = '';
		if(count($temp)==2){
			$value = trim($temp[1]);
			if($value=='false'){
				$value = false;
			}
			if($value=='true'){
				$value = true;
			}
		}							
		$pi_config[$var] = $value;	
	}
	
	$old_date_format = $pi_config['date_format'];
	if($old_date_format!='%Y-%m-%d' || $old_date_format!='%d-%m-%Y' || $old_date_format!='%d-%m-%y' || $old_date_format!='%y-%m-%d' || $old_date_format!='%m-%d-%y' || $old_date_format!='%m-%d-%Y'){
		//if the current dateformat is not one of the 6 formats, reset to Joomla's default format
		$updated_config = str_replace('date_format='.$old_date_format,'date_format=%Y-%m-%d', $updated_config);
		$config_needs_updating = 1;
	}	
	
	if($config_needs_updating){
		$database->setQuery( "UPDATE #__pi_config SET config='$updated_config' WHERE id='pi' ");
		$database->query();
	}
	
}

do_install_update_script();



?>
<div style="width: 500px; text-align: left;">
	<h2>Pages and Items</h2>
	<p>
		Thank you for using the Pages-and-Items framework for Joomla.
	</p>
	<p>
		Check <a href="http://www.pages-and-items.com" target="_blank">www.pages-and-items.com</a> for:
		<ul>
			<li>updates</li>
			<li>forum</li>
			<li>documentation</li>	
			<li>email notification service for updates and new extensions</li>				
		</ul>
	</p>
	<p>
		Component Pages-and-Items is only fully functional with the plugin and module installed and enabled.
	</p>
	<p>
		If you also install <a href="http://www.pages-and-items.com/admin-user-access/" target="_blank">component Admin-User-Access</a> you can manage users':
		<ul>
			<li>item access*</li>
			<li>category access*</li>
			<li>section access*</li>
			<li>page access*</li>
			<li>itemtype access*</li>
			<li>workflow</li>			
			<li>component access at backback</li>
			<li>module access at backback</li>
			<li>plugin access at backback</li>			
			<li>Admin-User-Access toolbar</li>
			<li>custom toolbar buttons per usergroup</li>
			<li>hide Joomla toolbar</li>
		</ul>
		*= when editting at backend or frontend
		<br />
		Admin-User-Access is NOT A HACK.
	</p>	
</div>